package com.ruoyi.livedemo.mapper;

import com.ruoyi.livedemo.entity.LifeRecord;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface LifeRecordMapper {

    @Select("SELECT * FROM life_records WHERE user_id = #{userId} " +
            "ORDER BY record_date DESC, id DESC " +
            "LIMIT #{limit} OFFSET #{offset}")
    List<LifeRecord> findPage(@Param("userId") Long userId,
                              @Param("offset") int offset,
                              @Param("limit") int limit);

    @Select("SELECT COUNT(1) FROM life_records WHERE user_id = #{userId}")
    long countByUser(Long userId);

    @Insert("INSERT INTO life_records (user_id, record_date, title, location, weather, mood, description, cover_path, attachment_id) " +
            "VALUES (#{userId}, #{recordDate}, #{title}, #{location}, #{weather}, #{mood}, #{description}, #{coverPath}, #{attachmentId})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(LifeRecord record);

    @Update("UPDATE life_records SET record_date=#{recordDate}, title=#{title}, location=#{location}, " +
            "weather=#{weather}, mood=#{mood}, description=#{description}, cover_path=#{coverPath}, attachment_id=#{attachmentId} " +
            "WHERE id=#{id} AND user_id=#{userId}")
    int update(LifeRecord record);

    @Delete("DELETE FROM life_records WHERE id=#{id} AND user_id=#{userId}")
    int delete(@Param("id") Long id, @Param("userId") Long userId);
}


